var CF=(function(){
	var file;
	return {
		sf:function(sf){
		   file=sf;
		},
		done:function(p){
			file&&file.done(p);
		}
	}
})();
function FILE(o){
	var T=this;
	T.ftp=o.ftp;
	T.div=util.$(o.div);
	T.input=util.$(o.input);
	T.fileinput=util.$(o.fileinput);
	T.rate=util.$(o.rate);
	o.suffix&&(T.suffix=o.suffix);
	T.line=null;
	
	T.inputchange=function(t){
		var T=t;
		var i = T.fileinput.value.lastIndexOf('\\');
		var name=T.fileinput.value;
		if(i>0){
			name=name.substring(i+1);
		}	
		if(T.suffix&&!name.match(T.suffix)){
    		alert("请选择"+T.suffix+"类型的文件.");
    	}else{
    		T.action=T.form.action;
	    	T.target=T.form.action||"";
			T.form.action="/resource/saveResource.shtml";
	    	T.form.target="ifrComposeAttachUpload";
	    	//alert(th);
	    	
			T.form.submit();
			CF.sf(T);
			T.line=setTimeout(T.startStatusCheck, 100); 
    	}

    };
    T.startStatusCheck=function(){
    	 function callback(data){
    	   var d = eval("("+data+")");
	       var b = true;
	       if(d){
	    	 T.rate.style.display="";
	    	 var t=parseInt(d.totalSize);var u=parseInt(d.readSize);
	    	 var r = t==0?100:Math.ceil(u*100/t);
	         T.rate.innerHTML=('<div style="width:'+r+'px;background: blue;float:left;">'+r+'%</div>');
	         d&&(r==100)&&(b=false);
	       }
	       b&&(T.line=setTimeout(T.startStatusCheck, 500));//((!T.done)&&(T.rate.innerHTML='<div style="width:100px;background: blue;float:left;">上传完成,处理中</div>'));    
    	 }    	
    	 new xhr("/resource/listener.shtml",callback).execute();
    };
    T.done=function(p){
    	  T.input.value=p;
    	  T.form.action=T.action;
    	  T.form.target="mainFrame";
    	  T.prew(p);
    	  clearTimeout(T.line);
    	  T.rate.innerHTML=('<div style="width:100px;background: green;">&nbsp;完成</div>');
    	  //T.done=true;
    };
    T.prew=function(p){
    	if(/.jpg|.png|.gif|.bmp/i.test(p)){
    		 T.imgDiv.innerHTML=('<img src='+T.ftp+p+' width="80" height="80" />');
    	}else if(/.mp3|.wav|.wma|.m4a|.aac/i.test(p)){
    		 T.imgDiv.innerHTML=('<img src="/images/music.jpg" width="80" height="80" />');
    	}else if(/.doc|.xls/i.test(p)){
    		 T.imgDiv.innerHTML=('<img src="/images/doc.jpg" width="80" height="80" />');
    	} else{
    		 T.imgDiv.innerHTML=('<img src="/images/def.jpg" width="80" height="80" />');
    	}
    };
    (function init(){		
    	if(T.input){
    		T.imgDiv=util.cElem('div',{cssText:"border:1px solid #ccc;height:80px;width:80px;",id:o.input+"div"});
    		insertAfter(T.imgDiv,T.input);
    		if(o.file && o.file=='file'){
    			T.imgDiv.style.display='none';
    		}
    		if(T.input.value.length!=0){
    		   T.prew(T.input.value);
    			//T.imgDiv.innerHTML=('<img src='+T.ftp+T.input.value+' width="80" height="80" />');
    		}
    	}
    	if(T.div){
    		T.form=T.fileinput.form;
    		if(T.fileinput){
    			util.add_event(T.fileinput,'change', util._apply(T.fileinput,T.inputchange,T) );
    		}
    	}
    })();
}


